//
//  XLHTTPSessionManager+Camgirl.m
//  XiangLove
//
//  Created by 陈宪栋 on 2017/11/30.
//  Copyright © 2017年 BAOMIHUA.COM. All rights reserved.
//

#import "XLHTTPSessionManager+Camgirl.h"
#import "AFNetworkActivityIndicatorManager.h"
#import <ReactiveCocoa.h>

#import <NSString+NSHash.h>

#import "XLCamgirlModel.h"
#import "NewCamgirlModel.h"
#import "XLPPUserInfoModel.h"
#import "XLPPGuardianModel.h"
#import "XLPPCamgirlVideoModel.h"
#import "HomeCallModel.h"
@implementation XLHTTPSessionManager (Camgirl)
+ (instancetype)sharedXIUQUManager {
    static dispatch_once_t onceToken;
    static XLHTTPSessionManager *instance;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] initWithBaseURL:[NSURL URLWithString:@"http://api.xiuquapp.com/"]];
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html",@"text/plain", @"text/json",nil];
    });
    return instance;
}

- (RACSignal *)XIUQUGET:(NSString *)URLString parameters:(NSDictionary *)parameters resultClass:(Class)resultClass {
    @weakify(self);
    RACSignal *requestSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        @strongify(self);
        NSURLSessionDataTask *task = [self GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            [subscriber sendNext:responseObject];
            [subscriber sendCompleted];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [subscriber sendError:error];
        }];
        return [RACDisposable disposableWithBlock:^{
            [task cancel];
        }];
    }];
    return [requestSignal flattenMap:^RACStream *(id data) {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            if (data == nil) {
                NSString *msg = @"空数据";
                NSInteger code = 803;
                NSError *error = [[NSError alloc] initWithDomain:@"error" code:code userInfo:@{NSLocalizedDescriptionKey:msg}];
                [subscriber sendError:error];
                return nil;
            }
            NSError *err;
            NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data
                                                               options:0
                                                                 error:&err];
            NSString *jsonString = [[NSString alloc] initWithData:jsonData
                                                         encoding:NSUTF8StringEncoding];
            BMHLog(@"%@", jsonString);
            NSInteger state = [[[data objectForKey:@"result"] objectForKey:@"errcode"] integerValue];
            id resultObj = [[data objectForKey:@"result"] objectForKey:@"data"];
            if (state == 0) {
                if ([resultObj isKindOfClass:[NSArray class]]) {
                    NSArray *resultArray = resultObj;
                    if (resultClass) {
                        resultObj = [[NSMutableArray alloc] init];
                        for (NSDictionary *dic in resultArray) {
                            id obj = [resultClass modelWithDictionary:dic];
                            [resultObj addObject:obj];
                        }
                    }
                }
                else if ([resultObj isKindOfClass:[NSDictionary class]]) {
                    if (resultClass) {
                        id obj = [resultClass modelWithDictionary:resultObj];
                        resultObj = obj;
                    }
                }
                [subscriber sendNext:resultObj];
                [subscriber sendCompleted];
            } else {
                NSString *msg = [[data objectForKey:@"result"] objectForKey:@"errmsg"];
                NSInteger code = [[[data objectForKey:@"result"] objectForKey:@"errcode"] integerValue];
                NSError *error = [[NSError alloc] initWithDomain:@"error" code:code userInfo:@{NSLocalizedDescriptionKey:msg}];
                [subscriber sendError:error];
            }
            return nil;
        }];
    }];
}

- (RACSignal *)getRandomCamgirlWithUserId:(NSString *)userId {
    NSString *URLString = @"/MobileInterface/PayVoiceVideo/PayVoiceVideo.ashx";
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:userId ?: @""     forKey:@"userId"];
    [parameters setObject:@"21"             forKey:@"type"];
    return [self XIUQUGET:URLString parameters:parameters resultClass:[XLCamgirlModel class]];
}

- (RACSignal *)getCamgirlListWithPageIndex:(NSInteger)pageIndex pageSize:(NSInteger)pageSize {
    NSString *URLString = @"/MobileInterface/payVoiceVideo/PayVoiceVideo.ashx";
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:@(pageIndex) forKey:@"pageIndex"];
    [parameters setObject:@(pageSize) forKey:@"pageSize"];
    [parameters setObject:@"20" forKey:@"type"];
    [parameters setObject:@(XQVersion) forKey:@"version"];
    return [self XIUQUGET:URLString parameters:parameters resultClass:[NewCamgirlModel class]];
}

-(RACSignal *)getNewCamgirlBeautyListPageIndex:(NSInteger)pageIndex type:(NSInteger)type userID:(NSString *)userId andSign:(NSString *)secretKey;
{
    NSString *URLString = @"/api/v1/VideoChatInfo/GetRecommendAnchorList";
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:userId ?: @""      forKey:@"userid"];
    [parameters setObject:@(pageIndex)       forKey:@"pageIndex"];
     [parameters setObject:@""               forKey:@"pageSize"];
    [self.requestSerializer setValue:secretKey ?: @"" forHTTPHeaderField:@"sign"];
    return [self GET:URLString parameters:parameters resultClass:[NewCamgirlModel class]];
}

- (RACSignal *)fetchGetBlacklistBuserId:(NSString *)buserId  UserId:(NSString *)UserId type:(NSString *)type {
    NSString *URLString = @"/MobileInterface/BlackList/GetBlacklist.ashx";
    long long timeStamp = [[NSDate date] timeIntervalSince1970];
    NSString *sign = [[F(@"uname=%lld", timeStamp) MD5] uppercaseString];
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    if (buserId) {
        [parameters setObject:buserId forKey:@"buserId"];
    }
    if (type) {
        [parameters setObject:type forKey:@"type"];
    }
    if (UserId) {
        [parameters setObject:UserId forKey:@"userId"];
    }
    [parameters setObject:@(timeStamp) forKey:@"timestamp"];
    [parameters setObject:sign forKey:@"sign"];
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        NSURLSessionDataTask *task = [self GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            [subscriber sendNext:responseObject];
            [subscriber sendCompleted];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [subscriber sendError:error];
        }];
        return [RACDisposable disposableWithBlock:^{
            [task cancel];
        }];
    }];
    
}

- (RACSignal *)getCamgirlUserInfoWithUserId:(NSString *)userId {
    NSString *URLString = @"/api/v1/AnchorIndex/GetAnchorInfo";
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:userId ?: @"" forKey:@"userid"];
    [parameters setObject:QudaoName forKey:@"qudao"];
    return [self GET:URLString parameters:parameters resultClass:[XLPPUserInfoModel class]];
}

- (RACSignal *)getUserGiveRankWithUserId:(NSString *)userId {//排行
    NSString *URLString = @"/api/v1/AnchorIndex/GetUserGiveRank";
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:userId ?: @"" forKey:@"userid"];
    [parameters setObject:QudaoName forKey:@"qudao"];
    return [self GET:URLString parameters:parameters resultClass:[XLPPGuardianModel class]];
}

- (RACSignal *)getCamgirlVideoListWithUserId:(NSString *)userId {
    NSString *URLString = @"/api/v1/AnchorIndex/GetAnchorDynamicVideo";
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:userId ?: @"" forKey:@"userid"];
    [parameters setObject:QudaoName forKey:@"qudao"];
    return [self GET:URLString parameters:parameters resultClass:[XLPPCamgirlVideoModel class]];
}

- (RACSignal *)makeCallWithAnchorId:(NSString *)anchorId userId:(NSString *)userId {
    //旧聊天接口
//    NSString *URLString = @"/api/v2/VideoChat/Connect1V1VideoChat";
    //新聊天接口
    NSString *URLString = @"/api/v2/VideoChat/Connect1V1VideoChatV3";
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:anchorId ?: @""   forKey:@"anchorId"];
    [parameters setObject:userId ?: @""     forKey:@"userId"];
    return [self XLV2GET:URLString parameters:parameters resultClass:[HomeCallModel class]];
}




@end
